package first.niuke.simple;

import java.util.ArrayList;

/**
 * @ClassName NC81
 * @description:
 * @author: 荣燊
 * @create: 2021-06-15 16:58
 **/
public class NC81 {
    // 时间复杂度有点大，后期改一下
    ArrayList<TreeNode> list = new ArrayList<>();

    void dfs(TreeNode root) {
      if (root == null) {
          return;
      }
      dfs(root.left);
      list.add(root);
      dfs(root.right);
    }

    // 二叉搜索树的第k个结点,前序遍历
    TreeNode KthNode(TreeNode pRoot, int k) {
        if (pRoot == null || k <= 0) {
            return null;
        }
        dfs(pRoot);
        TreeNode res = null;
        for (TreeNode temp : list) {
            if (k == 1) {
                res = temp;
                break;
            }
            k--;
        }
        return res;
    }



    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;

        }
    }

}
